﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WoChu.RfClient.Entity;
using WoChu.RfClient.Common;
using WoChu.CF.BaseFrame.Util;

namespace WoChu.RfClient.Duty
{
    public partial class FrmProdOnShelvesOrder : Form
    {
        public FrmProdOnShelvesOrder()
        {
            InitializeComponent();
        }
        private static List<GoodsBoxes> _lstGoodsBoxes = null;
        public static List<GoodsBoxes> lstGoodsBoxes
        {
            get
            {
                if (_lstGoodsBoxes == null) _lstGoodsBoxes = new List<GoodsBoxes>();
                return _lstGoodsBoxes;
            }
            set { _lstGoodsBoxes = value; }
        }

 
        private static List<OnShelvesInfo> lstOnShelves = null;
         
     
 
        //获取下架指令
        private void btnShelves_Click(object sender, EventArgs e)
        {

            try
            { 
                DateTime OnDT;
                FrmBindCommand frm = new FrmBindCommand();
                frm.ShowDialog();
                if (FrmDateTime.IsOK)
                     OnDT = Convert.ToDateTime(FrmBindCommand.BestTime.ToString("yyyy-MM-dd"));
                else
                    return;
 
                if (lstOnShelves!=null)
                {
                    if (MessageBox.Show("是否清空现有数据，重新获取", "提示消息", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
                        return;
                     
                }
                string batchNo =txtBatchNo.Text.Trim();
                var result=Duty.ProdOnShelvesOrderUIRule.NewInstance.GetOnShelvesInfo(FrmBindCommand.BatchNo, OnDT);
                if (result != null && result.RESULT == "1")
                    lstOnShelves = result.DATA;
                if (lstOnShelves == null)
                {
                   
                    MessageBox.Show("没有匹配条件的指令数据！");
                    return;
                }
                //else if(lstOnShelves.Count>0)
                //{
                //    iSort = lstOnShelves.Count;
                //}
                RefData();
            }
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex); 
            }
            finally
            {
                txtGoodsBatchCode.Focus();
                txtGoodsBatchCode.SelectAll();
            }
        }

        //private static int iSort = 0;
        //排序
        //private void Sort(OnShelvesInfo OnShelvesInfo)
        //{
        //    OnShelvesInfo.Sort = iSort;
        //    lstOnShelves.Sort((x, y) =>
        //    {
        //        return x.LOCATIONCODE.CompareTo(y.LOCATIONCODE);
        //    });
        //    this.dtGridOnShelves.Refresh();

        //    OnShelvesInfo.Sort = iSort++;
        //}
        //显亮当前行

        //private void SelectionBright()
        //{
        //    if (lstOnShelves == null )return;
        //    if (lstOnShelves.Count <= 0) return;
        //    //设置已扫描的为加亮
        //    if (string.IsNullOrEmpty(txtGoodsBatchCode.Text)) return;
        //    string barCode = txtGoodsBatchCode.Text.Trim();
        //    int curIndex = lstOnShelves.FindIndex(x => x.GOODSBATCHCODE == barCode);
        //    if (curIndex < 0) return;
        //    dtGridOnShelves.Select(curIndex);
        //    this.dtGridOnShelves.SelectionBackColor = Color.Green;
            
 
        //}
        //刷新数据

        private void RefData()
        {
            this.bsOnShelvesSource.DataSource = null;
            this.bsOnShelvesSource.DataSource = lstOnShelves;
            this.dtGridOnShelves.DataSource = this.bsOnShelvesSource;
            this.dtGridOnShelves.Refresh();

            this.dtGridGoxes.Visible = false;
            lstGoodsBoxes = null;
            this.bsBoxesSource = null;
            
        }
        
        //回车商品批次号
        private void txtGoodsBatchCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;

            if (string.IsNullOrEmpty(txtGoodsBatchCode.Text.Trim())) return;
            string gbCode = txtGoodsBatchCode.Text.Trim();
            //if (gbCode.Length != 18 && gbCode.Length != 17 && gbCode.Length != 20)
            //{
            //    MessageBox.Show("请扫描正确的商品批次条码！");
            //    txtGoodsBatchCode.SelectAll();
            //    return;
            //}
            try
            { 
                if(lstGoodsBoxes==null){
                    MessageBoxEx.Show("请先获取箱号数据");
                   txtBox.SelectAll();
                    return;
                }
                int currItem=lstGoodsBoxes.FindIndex(x=>x.GOODSCODE==gbCode);
                if (currItem >=0)
                {
                    dtGridGoxes.Select(currItem);
                }
                else
                {
                    MessageBoxEx.Show("该商品批次无匹配数据");
                    txtGoodsBatchCode.SelectAll();
                    return;
                }
                txtQty.Focus();
                txtQty.SelectAll();
            }
            catch (Exception ex)
            {
                txtGoodsBatchCode.Focus();
                txtGoodsBatchCode.SelectAll();
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
        }

        //回车数量
        private void txtQty_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;
 
            if(string.IsNullOrEmpty(txtQty.Text.Trim()))return;

            if (string.IsNullOrEmpty(txtGoodsBatchCode.Text.Trim())) return;
 
            try
            {
                try
                {
                    decimal qty = Convert.ToDecimal(txtQty.Text.Trim());
                }
                catch
                {
                    WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("输入的数量不正确!");
                    this.txtQty.Text = string.Empty;
                    this.txtQty.Focus();
                    return;
                }

                decimal OnQty = Convert.ToDecimal(txtQty.Text.Trim());
                if(OnQty<=0)
                {
                    MessageBox.Show("上架数量不能小于等于0！");
                    return;
                }
                string gbCode = txtGoodsBatchCode.Text.Trim();
                int curIndex = lstOnShelves.FindIndex(x => x.GOODSBATCHCODE == gbCode);
                if (curIndex >= 0)
                {
                    
                    lstGoodsBoxes[curIndex].INQTY= OnQty;
                    GoodsBoxes lastItem = lstGoodsBoxes[curIndex];
                    lstGoodsBoxes.RemoveAt(curIndex);
                    lstGoodsBoxes.Add(lastItem); 
                   
                    this.dtGridGoxes.Refresh(); 
 
                    txtQty.Text = string.Empty;
                    txtGoodsBatchCode.Text = string.Empty; 
                    txtGoodsBatchCode.Focus();
  
                }
                else
                {
                    txtGoodsBatchCode.Focus();
                    txtGoodsBatchCode.SelectAll();
                    WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("请重新扫描商品批次号！"); 
                }
               
            }
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
 
        } 
        //提交
        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (lstGoodsBoxes == null || lstGoodsBoxes.Count == 0)
                {
                    MessageBox.Show("数据为空，无法提交！");
                    return;
                }

                int isNullCount = lstGoodsBoxes.FindAll(x => !string.IsNullOrEmpty(x.GOODSCODE) && x.INQTY == 0).Count;
                if (isNullCount > 0)
                {
                    MessageBox.Show("有已扫描商品上架数量为0的数据，请检查！");
                    return;
                }
                //string gCode = null;
                foreach (GoodsBoxes  Item in lstGoodsBoxes)
                {


                    if (Item.INQTY != Item.OUTQTY)
                    {
                        if (MessageBox.Show("同一商品：" + Item.GOODSNAME + ",实下数[" + Item.OUTQTY + "]与箱中数[" + Item.INQTY + "]不相等,是否提交？", "提示消息", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
                        {
 
                            return;
                        }
                    }
                    if (Item.INQTY > Item.PLAYQTY)
                    {
                        if (MessageBox.Show("同一商品：" + Item.GOODSNAME + ",实下数[" + Item.OUTQTY + "]大于计划数[" + Item.PLAYQTY + "],是否提交？", "提示消息", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
                            return;
                    }
                }

                if (lstGoodsBoxes.Count() > 0)
                {
 
                    var re = Duty.ProdOnShelvesOrderUIRule.NewInstance.SetProdOnShelves(LoginBean.Instance.USERCODE,lstGoodsBoxes);
 
                    if (re.DATA)
                    {
                        lstOnShelves.Clear();
                        this.bsOnShelvesSource.DataSource = null;
                        this.dtGridOnShelves.DataSource = this.bsOnShelvesSource;
                        this.dtGridOnShelves.Refresh();

                        txtGoodsBatchCode.Text = string.Empty;
                        txtBatchNo.Text = string.Empty;
                        txtQty.Text = string.Empty;
                        txtBatchNo.Focus();

                        WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("指令下架提交成功！");
                    }
                    else
                    {
                        MessageBox.Show("提交失败！\n" + re.MESSAGE);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        //关闭窗体事件
        private void FrmOnShelvesOrder_Closed(object sender, EventArgs e)
        {
            lstOnShelves = null;
            lstGoodsBoxes = null;
            this.Close();
            this.Dispose();
        }
        //关闭窗体提示
        private void FrmOnShelvesOrder_Closing(object sender, CancelEventArgs e)
        {
            if (lstOnShelves != null)
            {
                if (lstOnShelves.Count > 0)
                {
                    if (MessageBox.Show("是否退出界面，将不保存界面数据？", "提示消息", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
                    {
                        e.Cancel = true;
                        return;
                    }
                }
            }
            
        }

  

        private void dtGridOnShelves_DoubleClick(object sender, EventArgs e)
        {
            int curIndex = dtGridOnShelves.CurrentRowIndex;
            if (curIndex < 0) return;
            try
            {
                string gbCode = dtGridOnShelves[curIndex, 6].ToString();
                OnShelvesInfo curItem = lstOnShelves.Find(x => x.GOODSBATCHCODE == gbCode);
                string ltCode = curItem.LOCATIONCODE;
                string gbName = curItem.GOODSNAME;
                
                string Message = string.Format("货位:{0}, 名称:{1}, 商品批次号:{2} \n 是否删除该数据？", ltCode, gbName, gbCode);
                if (MessageBoxEx.Question(Message) == DialogResult.Yes)
                {
                    lstOnShelves.Remove(curItem);
  
                    this.bsOnShelvesSource.DataSource = null;
                    this.bsOnShelvesSource.DataSource = lstOnShelves;
                    this.dtGridOnShelves.DataSource = this.bsOnShelvesSource;
                    this.dtGridOnShelves.Refresh();
                }
            }
            catch (Exception ex)
            {
                TraceEx.Write(ex.Message);
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show("删除数据失败!");
            }
            finally
            {
                txtGoodsBatchCode.Focus(); 
                txtGoodsBatchCode.SelectAll();
            }
        }

        //更新箱号绑定数据
        private void txtBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.Enter) return;

            if (string.IsNullOrEmpty(txtBox.Text.Trim())) return;
            try {
             
                string boxCode = txtBox.Text.Trim();
                GoodsBoxes lgitem = lstGoodsBoxes.Find(x => x.GOODSBOXCODE == boxCode);
                if (lgitem == null)
                {
                    var result=Duty.ProdOnShelvesUIRule.NewInstance.GetGoodsBoxesByBoxCode(boxCode);
                    List<GoodsBoxes> _GoodsBoxes = null;
                    if (result != null && result.RESULT == "1")
                        _GoodsBoxes = result.DATA;

                    if (_GoodsBoxes != null)
                    {
                        foreach (var item in _GoodsBoxes)
                        {
                            if (!lstGoodsBoxes.Any(x => x.GOODSID == item.GOODSID))
                                lstGoodsBoxes.Add(item);
                        }

                    }
                    else
                    {
                        MessageBox.Show("没有找到匹配的数据！");
                        txtBox.SelectAll();
                        return;
                    }
                }

                this.bsBoxesSource.DataSource = null;
                this.bsBoxesSource.DataSource = lstGoodsBoxes;
                this.dtGridGoxes.DataSource = this.bsBoxesSource;
                this.dtGridGoxes.Refresh();
                this.dtGridGoxes.Visible = true;
                this.dtGridOnShelves.Visible = false;

                txtGoodsBatchCode.Focus();
                txtGoodsBatchCode.SelectAll(); 
            }
            catch (Exception ex)
            {
                WoChu.CF.BaseFrame.Util.MessageBoxEx.Show(ex);
            }
        }
 
        
    }
}